var binfo = (function()
{
    var ua = navigator.userAgent.toLowerCase();
    return {
        ie : /*@cc_on!@*/false,
        i7 : /*@cc_on!@*/false && (parseInt(ua.match(/msie (\d+)/)[1], 10) >= 7),

        //获得页面属性
        getPageConf :function() {
            var db = document.body;
            var dd = document.documentElement;
            pageConf = {screenW:screen.width, screenH:screen.height,clientHD:dd.clientHeight,clientWD:dd.clientWidth,clientH:db.clientHeight,clientW:db.clientWidth,offsetW:db.offsetWidth,offsetH:db.offsetHeight};
            return pageConf;
        }

    };
})();

var config = { opac : 0.50, bgcolor : '#fff', bzi : null, it : null, il : null };

var tool =
{
    restyle : function(e)
    {
        e.style.cssText = 'margin:0;padding:0;background-image:none;background-color:transparent;border:0;';
    },

    ststyle : function(e, dict)
    {
        var style = e.style;
        for (var n in dict) style[n] = dict[n];
    },

    getestyle : function(e, p)
    {
        if (binfo.ie)
            return e.currentStyle[p];
        else
            return e.ownerDocument.defaultView.getComputedStyle(e, '').getPropertyValue(p);
    },

    stopac : function(e, opac)
    {
        if (binfo.ie)
        {
            opac = Math.round(opac * 100);
            e.style.filter = ( opac > 100 ? '' : 'alpha(opacity=' + opac + ')' );
        }
        else
            e.style.opacity = opac;
    },

    getvoid : function()
    {
        if (binfo.ie)
            return ( binfo.i7 ? '' : 'javascript:\'\'' );
        return 'javascript:void(0)';
    },
    //添加事件
    addevt : function(o, e, l)
    {
        if (binfo.ie)
            o.attachEvent('on' + e, l);
        else
            o.addEventListener(e, l, false);
    },
    //去除事件
    remevt : function(o, e, l)
    {
        if (binfo.ie)
            o.detachEvent('on' + e, l);
        else
            o.removeEventListener(e, l, false);
    },

    isdtd : function(doc)
    {
        return ( 'CSS1Compat' == ( doc.compatMode || 'CSS1Compat' ) );
    },

    getclsize : function(w)
    {
        if (binfo.ie)
        {
            var oSize, doc = w.document.documentElement;
            oSize = ( doc && doc.clientWidth ) ? doc : w.document.body;

            if (oSize)
                return { w : oSize.clientWidth, h : oSize.clientHeight };
            else
                return { w : 0, h : 0 };
        }
        else
            return { w : w.innerWidth, h : w.innerHeight };
    },

    getev : function()
    {
        if (binfo.ie) return window.event;
        var func = tool.getev.caller;
        while (func != null)
        {
            var arg = func.arguments[0];
            if (arg && (arg + '').indexOf('Event') >= 0) return arg;
            func = func.caller;
        }
        return null;
    },

    getepos : function(o)
    {
        var l, t;
        if (o.getBoundingClientRect)
        {
            var el = o.getBoundingClientRect();
            l = el.left;
            t = el.top;
        }
        else
        {
            l = o.offsetLeft - Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
            t = o.offsetTop - Math.max(document.documentElement.scrollTop, document.body.scrollTop);
        }
        return { x : l, y : t };
    },

    getspos : function(w)
    {
        if (binfo.ie)
        {
            var doc = w.document;
            var oPos = { X : doc.documentElement.scrollLeft, Y : doc.documentElement.scrollTop };
            if (oPos.X > 0 || oPos.Y > 0) return oPos;
            return { X : doc.body.scrollLeft, Y : doc.body.scrollTop };
        }
        else
            return { X : w.pageXOffset, Y : w.pageYOffset };
    },

    getlink : function(c)
    {
        if (c.length == 0) return;
        return '<' + 'link href="' + c + '" type="text/css" rel="stylesheet"/>';
    },

    regdoll : function(w)
    {
        if (binfo.ie)
            w.$ = w.document.getElementById;
        else
            w.$ = function(id) {
                return w.document.getElementById(id);
            };
    },

    getedoc : function(e)
    {
        return e.ownerDocument || e.document;
    },

    disctmenu : function(doc)
    {
        doc.oncontextmenu = function(e)
        {
            e = e || event || this.parentWindow.event;
            var o = e.srcElement || e.target;
            if (!( o.type == 'password' || o.type == 'text' || o.type == 'textarea' ))
            {
                if (binfo.ie) e.returnValue = false; else e.preventDefault();
            }
        };
    },

    getpath : function()
    {
        var bp, len, sc = document.getElementsByTagName('script');
        for (var i = 0; i < sc.length; i++)
        {
            bp = sc[i].src.substring(0, sc[i].src.toLowerCase().indexOf('lhgdialog.js'));
            len = bp.lastIndexOf('/');
            if (len > 0) bp = bp.substring(0, len + 1);
            if (bp) break;
        }
        if (binfo.ie && bp.indexOf('../') != -1)
        {
            var fp = window.location.href;
            fp = fp.substring(0, fp.lastIndexOf('/'));
            while (bp.indexOf('../') >= 0)
            {
                bp = bp.substring(3);
                fp = fp.substring(0, fp.lastIndexOf('/'));
            }
            return fp + '/' + bp;
        } else return bp;
    },

    //删除节点
    remnode : function(n) {
        return n.parentNode.removeChild(n);
    }
};

var lhgdialog = (function()
{
    var array_win = new Array();
    //添加数组项
    var Array_AddItem = function(item) {
        for (var i = 0; i < array_win.length; i++) {
            if (array_win[i] == item) return;
        }
        array_win.push(item);
    };

    //删除数组项
    var Array_RemoveItem = function(item) {
        var tmpArray = new Array();
        for (var i = 0; i < array_win.length; i++) {
            if (array_win[i] != item) tmpArray.push(array_win[i]);
        }
        array_win = tmpArray;
        tmpArray = null;
    };
    //判断是否在数组中存在
    var indexOfArray = function(value, arr)
    {
        if (!(typeof(value) == "string" || typeof(value) == "number")) return false;

        for (var i in arr)
        {
            if (arr[i] == value) return true;
        }
        return false;
    };
    var twin = window, cover;
    //循环找到最顶层
    //while( twin.parent && twin.parent != twin )
    //{
    //  try{ if( twin.parent.document.domain != document.domain ) break; } catch(e){break;}
    //twin = twin.parent;
    //}
    var tdoc = twin.document;

    //获取层次级别
    var getzi = function()
    {
        if (!config.bzi) config.bzi = 999;
        return ++config.bzi;
    };

    //设置窗口大小
    var resizehdl = function()
    {
        if (!cover) return;
        var rel = tool.isdtd(tdoc) ? tdoc.documentElement : tdoc.body;
        tool.ststyle(cover,
        {
            'width' : Math.max(rel.scrollWidth, rel.clientWidth, tdoc.scrollWidth || 0) - 1 + 'px',
            'height' : Math.max(rel.scrollHeight, rel.clientHeight, tdoc.scrollHeight || 0) - 1 + 'px'
        });
    };

    //重新设置窗口样式
    var reIframe = function(win, w, h, t, l) {
        tool.ststyle(win, { 'top' :  t,'left' : l,'width' : w, 'height' : h});
    }

    //获取元素对象
    var getElById = function(i) {
        return document.getElementById(i);
    }

    var oldWidth = "", oldHeight = "",oldTop = "",oldLeft = "",isNewWin = 0;
    var finalWidth = "", finalHeight = "",finalTop = "",finalLeft = "";

    //共有方法
    return {
        /**
         * 打开窗口方法
         * @param n 窗口id
         * @param t 窗口标题头内容
         * @param p 连接路径
         * @param w 窗口宽度
         * @param h 窗口高度
         * @param c 窗口蒙板
         * @param i 窗口是否是外部链接
         * @param s 窗口是否定位
         * @param l 窗口位置
         * @param o 窗口创建位置
          * @param q:窗口级别 one 一级 ,two 二级;
         */
        opendlg : function(n, t, p, w, h, c, i, s, l, o, q)
        {
            //判断窗口是否创建
            if (indexOfArray(n, array_win)) {
                isNewWin = 1;
            }
            if (isNewWin == 1) {
                getElById(n).style.display = "";
                if (cover) cover.style.display = "";
                getElById(n).style.zIndex=getzi(); //打开多个页面 切换
                return;
            }
            ;
            if (c) this.dcover(o); else {
                if (cover) cover = null;
            }
            var dinfo = { tit : t, page : p, win : window, topw : twin, link : i, sc : s ,vp : o};
            var clsize = tool.getclsize(twin), spos = tool.getspos(twin);

            var schdl = function()
            {
                var rel = tool.getspos(twin);
                tool.ststyle(dfrm, { 'top' : rel.Y + config.it + 'px', 'left' : rel.X + config.il + 'px' });
            };

            var it = ( l && l.top ) ? spos.Y + l.top : Math.max(spos.Y + ( clsize.h - h - 20 ) / 2, 0);
            var il = ( l && l.left ) ? spos.X + l.left : Math.max(spos.X + ( clsize.w - w - 20 ) / 2, 0);
            if (getElById(o)) { //webos 打开窗口 默认 top=0px
                it = 0;
            }
            if (getElById(o)) {
                getElById(o).style.zIndex = getzi() + 1;
                var dfrm = document.createElement("iframe");
            } else {
                dfrm = tdoc.createElement('iframe');
            }
            ;

            tool.restyle(dfrm);
            if (n) dfrm.id = n;
            if (n) dfrm.name = n;
            dfrm.frameBorder = 0;
            if (getElById(o)) {
                dfrm.src = tool.getpath() + 'webos.html';
            } else {
                if(q == "two"){
                   dfrm.src = tool.getpath() + 'lhgdialog_child.html';  //二级窗口
                }else{
                   dfrm.src = tool.getpath() + 'lhgdialog.html';  //一级窗口
                }
            }

            tool.ststyle(dfrm,
            {
                'position' : 'absolute', 'top' : it + 'px', 'left' : il + 'px',
                'width' : w + 'px', 'height' : h + 'px', 'zIndex' : getzi()
            });
            if (s) {
                config.it = it - spos.Y;
                config.il = il - spos.X;
            }
            ;
            if (s) tool.addevt(twin, 'scroll', schdl);
            if (getElById(o)) {
                getElById(o).appendChild(dfrm);
            } else {
                tdoc.body.appendChild(dfrm);
            }
            ;
            dfrm._dlgargs = dinfo;
            //将创建窗口加入数组
            Array_AddItem(n);
        },
        /**
         * 关闭窗口
         * @param d 窗口对象或窗口ID
         * @param c 蒙罩层
         */
        closdlg : function(d, c)
        {
            var dlg = ( 'object' == typeof(d) ) ? d.frameElement : document.getElementById(d);
            if (dlg) tool.remnode(dlg);
            if (c) this.hcover(c);
            //删除数组中创建窗口的id
            Array_RemoveItem(dlg.id);
            isNewWin = null;
            oldWidth = null;
            oldHeight = null;
            oldTop = null;
            oldLeft = null;
        },

        //获得活动窗口ID
        getActiveWinId : function() {
            var id,c = 0;
            if (array_win.length == 0) {
                return  "";
            }
            ;
            if (array_win.length == 1) {
                return array_win[0]
            }
            ;
            for (var i = 0; i < array_win.length; i++) {
                var a = getElById(array_win[i]).style.zIndex;
                if (a > c) {
                    c = a;
                    id = array_win[i];
                }
            }
            return id;
        },

        //最大化窗口
        isMax : false,
        winMax : function(win) {
            if (!win) return;
            var ifWidth = win.style.width;
            var ifHeight = win.style.height;
            var ifTop = win.style.top;
            var ifLeft = win.style.left;
            if (win.parentNode.tagName.toUpperCase() != "BODY") {
                var winWidth = win.parentNode.style.width;
                var winHeight = win.parentNode.style.height;
            } else {
                winWidth = document.body.clientWidth + 'px';
                winHeight = document.body.clientHeight - 5 + 'px';
            }
			if (!this.isMax && !this.isMin) {
                finalTop = ifTop;
                finalLeft = ifLeft;
                finalWidth = ifWidth;
                finalHeight = ifHeight;
                reIframe(win, winWidth, winHeight, '0px', '0px');
                oldTop = ifTop;
                oldLeft = ifLeft;
                oldWidth = ifWidth;
                oldHeight = ifHeight;
                this.isMax = true;
			}else if(this.isMax && !this.isMin){
                reIframe(win, oldWidth, oldHeight, oldTop, oldLeft);
                this.isMax = false;
			}else if(!this.isMax && this.isMin){
                reIframe(win, finalWidth, finalHeight, finalTop, finalLeft);
                this.isMin = false;
			}else if(this.isMax && this.isMin){
                reIframe(win, finalWidth, finalHeight, finalTop, finalLeft);
                this.isMax = false;
                this.isMin = false;
			}
        },

        //最小化窗口
        isMin : false,
        winMin : function(win,oBtn) {
            if (!win) return;
            var ifWidth = win.style.width;
            var ifHeight = win.style.height;
            var ifTop = win.style.top;
            var ifLeft = win.style.left;
			if (win.parentNode.tagName.toUpperCase() != "BODY") {
                var winWidth = win.parentNode.style.width;
                var winHeight = win.parentNode.style.height;
            } else {
                winWidth = document.body.clientWidth + 'px';
                winHeight = document.body.clientHeight - 5 + 'px';
            }
			if (!this.isMin && !this.isMax) {
                finalTop = ifTop;
                finalLeft = ifLeft;
                finalWidth = ifWidth;
                finalHeight = ifHeight;
				oBtn.title = '还原';
				oBtn.className = "obtn_active_over2";
                reIframe(win, ifWidth, '30px', '0px', '0px');
                oldTop = ifTop;
                oldLeft = ifLeft;
                oldWidth = ifWidth;
                oldHeight = ifHeight;
                this.isMin = true;
//			}else if(this.isMin && !this.isMax){
//                reIframe(win, oldWidth, oldHeight, oldTop, oldLeft);
//                this.isMin = false;
			}else if(!this.isMin && this.isMax){
                reIframe(win, ifWidth, '30px', '0px', '0px');
                this.isMin = true;
//			}else if(this.isMin && this.isMax){
//                reIframe(win, finalWidth, finalHeight, finalTop, finalLeft);
//                reIframe(win, winWidth, winHeight, '0px', '0px');
//                this.isMin = false;
			}
        },
//        //最小化窗口
//        winMin : function(win, c) {
//            if (!win) return;
//            if (c) {
//                c.style.display = "none"
//            }
//            ;
//            win.style.display = "none";
//        },

        //显示窗口
        showdlg : function(win) {
            if (!win) return;
            win.style.display = "";
        },

        //隐藏窗口
        hiddendlg : function(win) {
            if (!win) return;
            win.style.display = "none";
        },

        //创建蒙罩层
        dcover : function(o) {

            if (getElById(o)) {
                cover = document.createElement("div");
            } else {
                cover = tdoc.createElement('div');
            }
            ;
            tool.restyle(cover);
            tool.ststyle(cover,
            {
                'position' : 'absolute', 'zIndex' : getzi(), 'top' : '0px',
                'left' : '0px', 'backgroundColor' : config.bgcolor
            });
            tool.stopac(cover, config.opac);

            if (binfo.ie && !binfo.i7)
            {
                if (getElById(o)) {
                    var ifrm = document.createElement('iframe');
                } else {
                    ifrm = tdoc.createElement('iframe');
                }
                tool.restyle(ifrm);
                ifrm.hideFocus = true;
                ifrm.frameBorder = 10;
                ifrm.src = tool.getvoid();
                tool.ststyle(ifrm,
                {
                    'width' : '100%', 'height' : '100%', 'position' : 'absolute', 'left' : '0px',
                    'top' : '0px', 'filter' : 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)'
                });
                cover.appendChild(ifrm);
            }

            tool.addevt(twin, 'resize', resizehdl);
            resizehdl();
            if (getElById(o)) {
                getElById(o).appendChild(cover);
            } else {
                tdoc.body.appendChild(cover);
            }
            ;
        },
        //返回蒙罩层对象
        gcover : function() {
            return cover;
        },
        //销毁蒙罩层对象
        hcover : function(o) {
            tool.remnode(o);
            cover = null;
            o = null;
        }
    };
})();